import win32gui,win32com.client,win32con,win32api,psutil,win32print,win32process,wx

class Win32HandBiz(object):
  def __init__(this):
    this.hwnd = None
    this.shell = win32com.client.Dispatch("WScript.Shell")
    this.handLs = []
  # 获取屏幕大小 {width,height}
  def getScreenSize(this):
    return wx.GetDisplaySize()
      # hDC = win32gui.GetDC(0)
      # w = win32print.GetDeviceCaps(hDC, win32con.DESKTOPHORZRES)
      # h = win32print.GetDeviceCaps(hDC, win32con.DESKTOPVERTRES)
      # return (w,h)
  # 窗口标题
  def getHwndTitle(this,hwnd):
    return win32gui.GetWindowText(hwnd)
  # 进程名称如 chrome.exe
  def getHwndExeName(this,hwnd):
    hread_id, process_id = win32process.GetWindowThreadProcessId(hwnd)
    return psutil.Process(process_id).name()
  # 通过当前鼠标 -- 获取句柄
  def getHwndMouseCur(this):
    point = win32api.GetCursorPos()
    return win32gui.WindowFromPoint(point)
  # 通过标题  -- 获取句柄
  def getHwndByTitle(this,title):
    return win32gui.FindWindow(None,title)
  def setTopWindow(this,hwnd):
    this.shell.SendKeys('%')
    win32gui.SetForegroundWindow(hwnd)
  # 获取窗外位置
  def getWindowPosition(this,hwnd):
    left, top, right, bottom = win32gui.GetWindowRect(hwnd)
    ka = {'left':left,'top':top,'right':right,'bottom':bottom}
    print("left="+str(ka['left']))
  # 初始化所有句柄
  def initAllHand(this,exeName):
    this.exeName = exeName
    this.handLs.clear()
    win32gui.EnumWindows(this.onAllHand, 0)
    return this.handLs
  def onAllHand(this,hwnd, mouse):
    exeName = this.getHwndExeName(hwnd)
    if exeName == this.exeName:
      title = this.getHwndTitle(hwnd)
      # print(f'exe：{exeName}\t 名称： {title}')
      this.handLs.append({'exeName':exeName,'title':title,'hwnd':hwnd})


